package hu.lacimol.tutorial.todo.scheduling;

import hu.lacimol.tutorial.todo.service.TodoService;
import hu.lacimol.tutorial.todo.util.GlobalSettings;

import java.text.SimpleDateFormat;
import java.util.Map;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atlassian.sal.api.scheduling.PluginJob;

public class TodoTask implements PluginJob {
	
	private final static Logger logger = LoggerFactory.getLogger(TodoTask.class);
	
	@Override
	public void execute(Map<String, Object> jobDataMap) {
		
		final TodoMonitor monitor = (TodoMonitor) jobDataMap.get("TodoMonitorImpl:instance");
		final TodoService service = (TodoService) jobDataMap.get("TodoService");
		
		DateTime now = DateTime.now();
		SimpleDateFormat fmt = GlobalSettings.createHunDateFormatter();
		service.create("TodoTask description " + fmt.format(now.toDate()));
		monitor.setLastRun(now.toDate());
		monitor.setNextRun(now.plus(monitor.getInterval()).toDate());
		logger.info("TodoTask executed at " + fmt.format(monitor.getLastRun()) + " and will execute at "
				+ fmt.format(monitor.getNextRun()));
	}
}